# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter

import urllib.request

class ScrapyDytt835Pipeline:
    # open_spider()和close_spider()都为后面加的封装的方法，用于新建打开文件和关闭文件,'w'以打开的状态写入
    def open_spider(self,spider):
        self.fp = open('movie.json','w',encoding='utf-8')

    def process_item(self, item, spider):
        self.fp.write(str(item))

        return item

    def close_spider(self,spider):
        self.fp.close()

# 加个管道用于下载图片
class Dytt8DownloadPineline:

    def process_item(self, item, spider):
        filename = './movieImg/' + item.get('name') + '.jpg'
        url = item.get('src')

        urllib.request.urlretrieve(url=url,filename=filename)